/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package hanuma;

import java.net.*;
import java.util.*;
import javax.net.ssl.*;
import com.google.gson.*;

import java.io.*;

/**
 *
 * @author niyaz
 */
public class VKontakteApi {

    private String token;
    private String user;

    public VKontakteApi(String token, String user) {
        this.token = token;
        this.user = user;
    }

    private static String readAll(Reader rd) throws IOException {
        StringBuilder sb = new StringBuilder();
        int cp;
        int open = '{';
        int close = '}';
        int sum = 0;
        while ((cp = rd.read()) != -1) {
            sb.append((char) cp);
        }
        return sb.toString();
    }

    /*  public Person getPerson(String user){

    }*/
    public List<String> getFriends(String user) throws Exception {
        List<String> friends = new LinkedList<String>();
        try {

            //https://api.vkontakte.ru/method/friends.get?uid=7359953&access_token=6a25adf26a55e0236a55e023ef6a55e9bd66a556a52489d2d689eccf519945c
            String req = new String("https://api.vkontakte.ru/method/friends.get?uid=" + user + "&access_token=" + token);
            // System.out.println(req);
            BufferedReader r = new BufferedReader(new InputStreamReader((new URL(req)).openStream()));
            String answer = readAll(r);
            Gson gson = new Gson();
            JsonElement all = gson.fromJson(answer, com.google.gson.JsonElement.class);
            JsonArray array = all.getAsJsonObject().getAsJsonArray("response");
            // System.out.println(array);
            for (JsonElement el : array) {
                friends.add(Integer.toString(el.getAsInt()));
            }
        } catch (Exception e) {
        }

        return friends;
    }

    public List<WallRecord> getWall(String user) throws Exception {
        List<WallRecord> wall = new LinkedList<WallRecord>();
        try {
            String req = new String("https://api.vkontakte.ru/method/wall.get?owner_id=" + user + "&offset=0&count=1&access_token=" + token);
            BufferedReader r = new BufferedReader(new InputStreamReader((new URL(req)).openStream()));
            String answer = readAll(r);
            Gson gson = new Gson();
            JsonElement all = gson.fromJson(answer, com.google.gson.JsonElement.class);
            JsonArray array = all.getAsJsonObject().getAsJsonArray("response");
            int count = array.get(0).getAsInt();
            int offset = 0;
            while (count > 0) {
                req = new String("https://api.vkontakte.ru/method/wall.get?owner_id=" + user + "&offset=" + offset + "&count=100&access_token=" + token);
                offset += 100;
                count -= 100;
                r = new BufferedReader(new InputStreamReader((new URL(req)).openStream()));
                answer = readAll(r);
                gson = new Gson();
                all = gson.fromJson(answer, com.google.gson.JsonElement.class);
                array = all.getAsJsonObject().getAsJsonArray("response");

                for (int i = 1; i < array.size(); i++) {
                    JsonElement e = array.get(i);
                    //  System.out.println(e);
                    WallRecord record = new WallRecord();
                    record.commentsNumber = e.getAsJsonObject().get("comments").getAsJsonObject().get("count").getAsInt();
                    record.likes = e.getAsJsonObject().get("likes").getAsJsonObject().get("count").getAsInt();
                    record.creator = e.getAsJsonObject().get("from_id").getAsString();
                    {
                        record.copy = false;
                        if (e.getAsJsonObject().has("copy_owner_id")) {
                            record.copy = true;
                        }
                    }
                    record.reposts = e.getAsJsonObject().get("reposts").getAsJsonObject().get("count").getAsInt();
                    record.text = e.getAsJsonObject().get("text").toString();
                    if (e.getAsJsonObject().has("title")) {
                        record.text = e.getAsJsonObject().get("title").toString() + " @@@ " + record.text;
                    }
                    record.time = new Date(e.getAsJsonObject().get("date").getAsInt());
                    wall.add(record);
                }
            }
        } catch (Exception e) {
        }
        return wall;
    }

    public List<String> getGroups(String user) throws Exception {
        if (user == null) {
            throw new Exception("user can't be null");
        }
        List<String> groups = new LinkedList<String>();
        String req = new String("https://api.vkontakte.ru/method/groups.get?uid=" + user + "&extended=1&access_token=" + token);
        try {
            BufferedReader r = new BufferedReader(new InputStreamReader((new URL(req)).openStream()));
            String answer = readAll(r);
            Gson gson = new Gson();
            JsonElement all = gson.fromJson(answer, com.google.gson.JsonElement.class);
            JsonArray array = all.getAsJsonObject().getAsJsonArray("response");
            int count = array.get(0).getAsInt();
            //System.out.println(count);
            for (int i = 0; i < count; i++) {
                JsonElement e = array.get(i + 1);
                //  System.out.println(e);
                JsonElement el = e.getAsJsonObject().get("name");
                groups.add(el.getAsString());

            }
        } catch (Exception e) {
            // most of the time, security problems
        }
        return groups;
    }

    public int getCity(String user) throws Exception {
        String req = new String("https://api.vkontakte.ru/method/getProfiles?uid=" + user + "&fields=city&access_token=" + token);
        //   System.out.println(req);
        BufferedReader r = new BufferedReader(new InputStreamReader((new URL(req)).openStream()));
        String answer = readAll(r);
        Gson gson = new Gson();
        JsonElement all = gson.fromJson(answer, com.google.gson.JsonElement.class);
        JsonArray array = all.getAsJsonObject().getAsJsonArray("response");
        //    System.out.println(array.toString());
        for (JsonElement e : array) {
            if (e.getAsJsonObject().has("city")) {
                //           System.out.println(e.getAsJsonObject().get("city").getAsInt());
                return e.getAsJsonObject().get("city").getAsInt();
            }
        }
    return 0;
    }

    public int getGraduation(String user) throws Exception {
        String req = new String("https://api.vkontakte.ru/method/getProfiles?uid=" + user + "&fields=education&access_token=" + token);
        // System.out.println(req);
        BufferedReader r = new BufferedReader(new InputStreamReader((new URL(req)).openStream()));
        String answer = r.readLine();
        Gson gson = new Gson();
        JsonElement all = gson.fromJson(answer, com.google.gson.JsonElement.class);
        JsonArray array = all.getAsJsonObject().getAsJsonArray("response");
        //    System.out.println(array.toString());
        for (JsonElement e : array) {
            if (e.getAsJsonObject().has("graduation")) {
                //       System.out.println(e.getAsJsonObject().get("graduation").getAsInt());
                return e.getAsJsonObject().get("graduation").getAsInt();
            }
        }
        //  System.out.println(answer);
        throw new Exception("can't do education");
    }

    public int getSex(String user) throws Exception {
        String req = new String("https://api.vkontakte.ru/method/getProfiles?uid=" + user + "&fields=sex&access_token=" + token);
        // System.out.println(req);
        BufferedReader r = new BufferedReader(new InputStreamReader((new URL(req)).openStream()));
        String answer = r.readLine();
        Gson gson = new Gson();
        JsonElement all = gson.fromJson(answer, com.google.gson.JsonElement.class);
        JsonArray array = all.getAsJsonObject().getAsJsonArray("response");
        //    System.out.println(array.toString());
        for (JsonElement e : array) {
            if (e.getAsJsonObject().has("sex")) {
                //       System.out.println(e.getAsJsonObject().get("graduation").getAsInt());
                return e.getAsJsonObject().get("sex").getAsInt();
            }
        }
        //  System.out.println(answer);
        throw new Exception("can't do sex");
    }

    public String getLastName(String user) throws Exception {
        String req = new String("https://api.vkontakte.ru/method/getProfiles?uid=" + user + "&fields=city&access_token=" + token);
        //System.out.println(req);
        BufferedReader r = new BufferedReader(new InputStreamReader((new URL(req)).openStream()));
        String answer = readAll(r);
        Gson gson = new Gson();
        JsonElement all = gson.fromJson(answer, com.google.gson.JsonElement.class);
        JsonArray array = all.getAsJsonObject().getAsJsonArray("response");
        //    System.out.println(array.toString());
        for (JsonElement e : array) {
            if (e.getAsJsonObject().has("last_name")) {
                //  System.out.println(e.getAsJsonObject().get("graduation").getAsInt());
                return e.getAsJsonObject().get("last_name").getAsString();
            }
        }
        throw new Exception("can't do lastname");
    }

    public List<String> getAlbums(String user) throws Exception {
        List<String> albums = new LinkedList<String>();
        String req = new String("https://api.vkontakte.ru/method/photos.getAlbums?uid=" + user + "&access_token=" + token);
        try {
            BufferedReader r = new BufferedReader(new InputStreamReader((new URL(req)).openStream()));
            String answer = readAll(r);
            Gson gson = new Gson();
            JsonElement all = gson.fromJson(answer, com.google.gson.JsonElement.class);
            JsonArray array = all.getAsJsonObject().getAsJsonArray("response");
            //System.out.println(count);
            for (JsonElement e : array) {
                //  System.out.println(e);
                JsonElement el = e.getAsJsonObject().get("title");
                albums.add(el.getAsString());

            }
        } catch (Exception e) {
            // most of the time, security problems
        }
        return albums;

    }

    public List<String> getMusic(String user) throws Exception {
        List<String> music = new LinkedList<String>();
        String req = new String("https://api.vkontakte.ru/method/audio.get?uid=" + user + "&access_token=" + token);
        try {
            BufferedReader r = new BufferedReader(new InputStreamReader((new URL(req)).openStream()));
            String answer = readAll(r);
            Gson gson = new Gson();
            JsonElement all = gson.fromJson(answer, com.google.gson.JsonElement.class);
            JsonArray array = all.getAsJsonObject().getAsJsonArray("response");
            //System.out.println(count);
            for (JsonElement e : array) {
                //  System.out.println(e);
                JsonElement artist = e.getAsJsonObject().get("artist");
                JsonElement title = e.getAsJsonObject().get("title");

                music.add(artist.getAsString() + " " + title.getAsString());
            }
        } catch (Exception e) {
            // most of the time, security problems
        }
        return music;

    }

    public List<String> getNotes(String user) throws Exception {
        List<String> music = new LinkedList<String>();
        String req = new String("https://api.vkontakte.ru/method/notes.get?uid=" + user + "&access_token=" + token);
        try {
            BufferedReader r = new BufferedReader(new InputStreamReader((new URL(req)).openStream()));
            String answer = readAll(r);
            Gson gson = new Gson();
            JsonElement all = gson.fromJson(answer, com.google.gson.JsonElement.class);
            JsonArray array = all.getAsJsonObject().getAsJsonArray("response");
            //System.out.println(count);
            boolean first = true;
            for (JsonElement e : array) {
                if (first) {
                    first = false;
                    continue;
                }
                //  System.out.println(e);
                JsonElement title2 = e.getAsJsonObject().get("title");
                JsonElement text = e.getAsJsonObject().get("text");

                music.add(title2.getAsString() + " " + text.getAsString());
            }
        } catch (Exception e) {
            // most of the time, security problems
            //    e.printStackTrace();
        }
        return music;

    }

    public List<String> getVideo(String user) throws Exception {
        List<String> music = new LinkedList<String>();
        String req = new String("https://api.vkontakte.ru/method/video.get?uid=" + user + "&access_token=" + token);
        try {
            BufferedReader r = new BufferedReader(new InputStreamReader((new URL(req)).openStream()));
            String answer = readAll(r);
            Gson gson = new Gson();
            JsonElement all = gson.fromJson(answer, com.google.gson.JsonElement.class);
            JsonArray array = all.getAsJsonObject().getAsJsonArray("response");
            //System.out.println(count);
            boolean first = true;
            for (JsonElement e : array) {
                if (first) {
                    first = false;
                    continue;
                }
                //  System.out.println(e);
                JsonElement title = e.getAsJsonObject().get("title");
                if (e.getAsJsonObject().has("description")) {
                    music.add(title.getAsString() + " " + e.getAsJsonObject().get("description").getAsString());
                } else {
                    music.add(title.getAsString());
                }

            }
        } catch (Exception e) {
            // e.printStackTrace();
        }
        return music;

    }

    public int getTotalPhotoNumber(String user) throws Exception {
        String req = new String("https://api.vkontakte.ru/method/photos.getAll?owner_id=" + user + "&offset=0&count=1&access_token=" + token);
        int count = 0;
        try {
            BufferedReader r = new BufferedReader(new InputStreamReader((new URL(req)).openStream()));
            String answer = readAll(r);
            Gson gson = new Gson();
            JsonElement all = gson.fromJson(answer, com.google.gson.JsonElement.class);
            JsonArray array = all.getAsJsonObject().getAsJsonArray("response");
            count = array.get(0).getAsInt();
        } catch (Exception e) {
        }
        return count;

    }

    public Person getPerson(String user) throws Exception {
        Person person = new Person();
        person.ID = user;
        person.albums = this.getAlbums(user);
        person.friends = this.getFriends(user);
        person.groups = this.getGroups(user);
        person.music = this.getMusic(user);
        person.name = this.getLastName(user);
        person.notes = this.getNotes(user);
        person.numberOfFotos = this.getTotalPhotoNumber(user);
        // person.univers = null;
        person.video = this.getVideo(user);
        person.wall = this.getWall(user);
        return person;
    }
}
